using System;
using System.Collections;
using System.Collections.Generic;
using System.Transactions;
using UnityEngine;

public class ProjectilStandard : MonoBehaviour
{
    public float maxLifeTime = 5f;
    public float speed = 300f;

    public Transform root;
    public Transform tip;
    public float radius = 0.01f;
    public LayerMask hittableLayers = -1;

    public GameObject impactVFX;
    public float impactVFXLifeTime = 5f;
    public float impactVFXSpawnOffset = 0.1f;
    
    private ProjectileBase _projectileBase;
    private Vector3 _velocity;
    private Vector3 _lastRootPosition;

    // private bool _hasTrajectoryCorrected;
    // private Vector3 _correctionVector;//校准弹道方向变量

    private Vector3 _consumedCorrectionVector;

    public float trajectoryCorrectionDistance = 5f;
    private void OnEnable()
    {
        _projectileBase = GetComponent<ProjectileBase>();
        _projectileBase.onShoot += Onshoot;
        Destroy(gameObject, maxLifeTime);
    }

    private void Onshoot()
    {
        _velocity += transform.forward * speed;
        _lastRootPosition = root.position;

        PlayerWeaponManager playerWeaponManager = _projectileBase.owner.GetComponent<PlayerWeaponManager>();

        // if (playerWeaponManager)
        // {
        //     _hasTrajectoryCorrected = false;
        //
        //     Transform weaponCameraTransform = playerWeaponManager.weaponCamera.transform;
        //
        //     Vector3 cameraTomuzzle = _projectileBase.initialPosition - weaponCameraTransform.position;
        //     
        //     _correctionVector = Vector3.ProjectOnPlane(-cameraTomuzzle, weaponCameraTransform.forward);
        //     
        // }
    }

    private void Update()
    {
        //移动
        transform.position += _velocity * Time.deltaTime;
        //方向
        transform.forward = _velocity.normalized;
        
        //矫正子弹准心
        // if (!_hasTrajectoryCorrected&& _consumedCorrectionVector.sqrMagnitude<_consumedCorrectionVector.sqrMagnitude)
        // {
        //     Vector3 correctionLeft = _correctionVector - _consumedCorrectionVector;
        //     float distanceThisFrame = (root.position - _lastRootPosition).magnitude;
        //
        //     Vector3 correctionThisFram = (distanceThisFrame / trajectoryCorrectionDistance) * _correctionVector;
        //     correctionThisFram = Vector3.ClampMagnitude(correctionThisFram, correctionLeft.magnitude);
        //     _consumedCorrectionVector += correctionThisFram;
        //
        //     if (Mathf.Abs(_consumedCorrectionVector.sqrMagnitude - _correctionVector.sqrMagnitude) < Mathf.Epsilon)
        //     {
        //         _hasTrajectoryCorrected = true;
        //     }
        //
        //     transform.position += correctionThisFram;
        // }
        //

        RaycastHit closesHit = new RaycastHit();
        closesHit.distance = Mathf.Infinity;
        bool foundHit = false;

        Vector3 displacementSinceLastFrame = tip.position - _lastRootPosition;
        RaycastHit[] hits = Physics.SphereCastAll(_lastRootPosition,radius,
            displacementSinceLastFrame.normalized,
            displacementSinceLastFrame.magnitude,
            hittableLayers,
            QueryTriggerInteraction.Collide);
        foreach (RaycastHit hit in hits)
        {
            if (isHitValid(hit) && hit.distance < closesHit.distance)
            {
                closesHit = hit;
                foundHit = true;
            }
        }

        if (foundHit)
        {
            if (closesHit.distance<=0)
            {
                closesHit.point = root.position;
                closesHit.normal = -transform.forward;
            }

            onHit(closesHit.point,closesHit.normal);
        }
    }

    private void onHit(Vector3 point,Vector3 normal)
    {
        if (impactVFX != null)
        {
            GameObject impactVFSInstance = Instantiate(impactVFX, point + normal * impactVFXSpawnOffset,
                Quaternion.LookRotation(normal));

            if (impactVFXLifeTime > 0)
            {
                Destroy(impactVFSInstance,impactVFXLifeTime);
            }
        }
        
        print("Hit");
        
        Destroy(gameObject);
    }

    private bool isHitValid(RaycastHit hit)
    {
        if (hit.collider.isTrigger)
        {
            return false;
        }

        return true;
    }
}
